<?php

namespace app\mobile\controller;

use app\BaseController;

use app\Request;
use think\db\Query;
use think\facade\Validate;
use think\facade\Db;

use app\mobile\model\Answer;
use app\mobile\model\AnswerInfo;
use app\mobile\model\AnswerScore;
use app\mobile\model\AnswerTarget;
use app\mobile\model\AnswerTargetScore;
use app\mobile\model\User;
use app\mobile\model\TargetBase;
use app\mobile\model\TargetDetail;
use app\mobile\model\TargetLink;


/*
 * 答题
 */
class AnswerController extends BaseController
{

    /**
     * 提交答题
     * @fun:
     * @author:xfwed
     * @time:2023/06/20 10:14
     */
    public function submitAnswer(){
        $param = input('param.');
        $validate = Validate::rule([
            'user_id|用户id' => 'require',
            'use_time|用时' => 'require',
            'topic_num|题目数' => 'require',
        ]);
        if (!$validate->check($param)) {
            $error = $validate->getError();
            return $this->err_json($error,'default');
        }
        $user_id = $param['user_id'];
        $use_time = $param['use_time'];
        $topic_num = $param['topic_num'];
        Db::startTrans();
        //1.添加  答题表
        $answerData['user_id'] = $user_id;
        $com_id = User::where('id',$user_id)->value('com_id');
        $answerData['com_id'] = $com_id;
        $answerData['use_time'] = $use_time;
        $answerData['topic_num'] = $topic_num;
        $answerData['update_time'] = gD();
        $answerData['add_time'] = gD();
        $addAnswer = Answer::insertGetId($answerData);
        //2.添加  答题详细表
        $aiData = [];
        foreach ($param['answer'] as $k => $v){
            $data['answer_id'] = $addAnswer;
            $data['topic_id'] = $v['topic_id'];
            $data['topic_option'] = $v['topic_option'];
            $data['score'] = $v['score'];
            $data['target_id'] = $v['target'];
            array_push($aiData,$data);
        }
        $addAnswerInfo = AnswerInfo::insertAll($aiData);
        //3.添加  标准分-指标计分
        $baseList = TargetBase::where('is_del',2)
                ->field('id,name,letter,conversion')
                ->select();
        $asData = [];
        foreach ($baseList as $k => $v){
            $id = $v['id'];
            $data1['answer_id'] = $addAnswer;
            $data1['target_id'] = $v['id'];
            $origin_score = AnswerInfo::where('answer_id',$addAnswer)
                    ->where('target_id',$id)
                    ->sum('score');
            $data1['origin_score'] = $origin_score;
            //原始分换算标准分
            $norm_score = AnswerScore::sorts($v['conversion'], $origin_score);
            $data1['norm_score'] = $norm_score;
            array_push($asData,$data1);
         }
         $addAnswerScore = AnswerScore::insertAll($asData);
        //4.添加  结果勾兑明细
        $anwserTarget = TargetLink::alias('tl')
                ->join('target_detail td','td.id = tl.detail_id','left')
                ->where('td.is_del',2)
                ->where('td.com_id','in','0,'.$com_id)
                ->order('tl.id asc')
                ->field('tl.id,tl.base_id,tl.detail_id,tl.num')
                ->select();
        $atData = [];
        foreach ($anwserTarget as $k => $v){
            $data2['answer_id'] = $addAnswer;
            $data2['base_id'] = $v['base_id'];
            $data2['detail_id'] = $v['detail_id'];
            $score = AnswerScore::where('answer_id',$addAnswer)
                    ->where('target_id',$v['base_id'])
                    ->value('norm_score');
            $num = $v['num']*$score;
            $data2['num'] = $num;
            array_push($atData,$data2);
        }
        $addAnswerTarget = AnswerTarget::insertAll($atData);
        //5.添加  结果勾兑标准分 
        $targetDetail = TargetDetail::where('pid','<>',0)
                ->where('com_id','in','0,'.$com_id)
                ->where('is_del',2)
                ->field('id detail_id')
                ->select();
        $atsData = [];
        foreach ($targetDetail as $k => $v){
            $data3['answer_id'] = $addAnswer;
            $data3['detail_id'] = $v['detail_id'];
            //简单求和
            $short_sum = AnswerTarget::where('answer_id',$addAnswer)
                    ->where('detail_id',$v['detail_id'])
                    ->sum('num');
            $data3['short_sum'] = $short_sum;
            //负数校正
            $minus_num = AnswerTarget::where('answer_id',$addAnswer)
                    ->where('detail_id',$v['detail_id'])
                    ->where('num','<',0)
                    ->count();
            $minus = $minus_num*11;
            $data3['minus'] = $minus;
            //总和校正
            $data3['check_sum'] = $short_sum + $minus;
            $data3['weight'] = TargetDetail::where('id',$v['detail_id'])->value('weight');
            $data3['norm_score'] = $data3['check_sum']/$data3['weight'];
            array_push($atsData,$data3);
        }
        $addAnswerTargetScore = AnswerTargetScore::insertAll($atsData);
        
        if($addAnswer && $addAnswerInfo && $addAnswerScore && $addAnswerTarget && $addAnswerTargetScore){
            Db::commit();
            return $this->suc_json('提交成功',$addAnswer);
        }  else {
            Db::rollback();
            return $this->err_json('提交失败-'.$addAnswer.'-'.$addAnswerInfo.'-'.$addAnswerScore.'-'.$addAnswerTarget.'-'.$addAnswerTargetScore);
        }
    }

}